﻿using System;
using System.Data;
using System.Text;

namespace ltModel.DAL
{
    /// <summary>
    /// 数据访问类leadTeam。
    /// </summary>
    public class leadTeam
    {
        public leadTeam()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int tid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from lt_leadTeam where tid=" + tid + "");
            return DataObject.Exists(strSql.ToString());
        }

        /// <summary>
        /// 返回类别最大序号
        /// </summary>
        /// <param name="sort">分类</param>
        /// <returns></returns>
        public int MaxPx(string sort)
        {
            string sql = "SELECT MAX(px) FROM lt_leadTeam ";
            if (sort.Trim() != "")
                sql = sql + " where sot=" + sort;
            DataTable maxdt = DataObject.Query(sql).Tables[0];
            if (maxdt.Rows[0][0].ToString() != "")
            {
                return (Int32.Parse(maxdt.Rows[0][0].ToString()) + 1);
            }
            else
            {
                return 1;
            }
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ltModel.Model.leadTeam model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into lt_leadTeam(");
            strSql.Append("tname,duty,tel,email,area,pic,addtime,px,sot,beizhu");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("'" + model.tname + "',");
            strSql.Append("'" + model.duty + "',");
            strSql.Append("'" + model.tel + "',");
            strSql.Append("'" + model.email + "',");
            strSql.Append("'" + model.area + "',");
            strSql.Append("'" + model.pic + "',");
            strSql.Append("'" + model.addtime + "',");
            strSql.Append("" + model.px + ",");
            strSql.Append("" + model.sot + ",");
            strSql.Append("'" + model.beizhu + "'");
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DataObject.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ltModel.Model.leadTeam model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update lt_leadTeam set ");
            if (model.tname != null)
                strSql.Append("tname='" + model.tname + "',");
            if (model.duty != null)
                strSql.Append("duty='" + model.duty + "',");
            if (model.tel != null)
                strSql.Append("tel='" + model.tel + "',");
            if (model.email != null)
                strSql.Append("email='" + model.email + "',");
            if (model.area != null)
                strSql.Append("area='" + model.area + "',");
            if (model.pic != null)
                strSql.Append("pic='" + model.pic + "',");
            if (model.px.Equals(Int32.Parse("0")) != true)
                strSql.Append("px=" + model.px + ",");
            if (model.sot.Equals(Int32.Parse("0")) != true)
                strSql.Append("sot=" + model.sot + ",");
            if (model.beizhu != null)
                strSql.Append("beizhu='" + model.beizhu + "',");
            strSql.Append(" addtime=addtime ");
            strSql.Append(" where tid=" + model.tid + "");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 领导排序上下移
        /// </summary>
        /// <param name="id">ID</param>
        /// <param name="type">上移，下移</param>
        public void PxMove(int id, string type)
        {
            StringBuilder strSql = new StringBuilder();
            if (type.Trim() == "down")
            {
                strSql.Append("update lt_leadTeam set px = (select px from lt_leadTeam where tid=" + id.ToString() + ") where tid = (select top 1 tid from lt_leadTeam where px>(select px from lt_leadTeam where tid=" + id.ToString() + ") order by px asc) ");
                strSql.Append(" update lt_leadTeam set px = px+1 where tid=" + id.ToString());
            }
            else
            {
                strSql.Append("update lt_leadTeam set px = (select px from lt_leadTeam where tid=" + id.ToString() + ") where tid = (select top 1 tid from lt_leadTeam where px<(select px from lt_leadTeam where tid=" + id.ToString() + ") order by px desc) ");
                strSql.Append(" update lt_leadTeam set px = px-1 where tid=" + id.ToString());
            }
            DataObject.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int tid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete lt_leadTeam ");
            strSql.Append(" where tid=" + tid);
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ltModel.Model.leadTeam GetModel(int tid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ");
            strSql.Append(" [tid],[tname],[duty],[tel],[email],[area],[pic],[addtime],[px],[sot],[beizhu] ");
            strSql.Append(" from lt_leadTeam ");
            strSql.Append(" where tid=" + tid);
            ltModel.Model.leadTeam model = new ltModel.Model.leadTeam();
            DataSet ds = DataObject.Query(strSql.ToString());
            model.tid = tid;
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.tname = ds.Tables[0].Rows[0]["tname"].ToString();
                model.duty = ds.Tables[0].Rows[0]["duty"].ToString();
                model.tel = ds.Tables[0].Rows[0]["tel"].ToString();
                model.email = ds.Tables[0].Rows[0]["email"].ToString();
                model.area = ds.Tables[0].Rows[0]["area"].ToString();
                model.pic = ds.Tables[0].Rows[0]["pic"].ToString();
                model.addtime = ds.Tables[0].Rows[0]["addtime"].ToString();

                if (ds.Tables[0].Rows[0]["px"].ToString() != "")
                {
                    model.px = int.Parse(ds.Tables[0].Rows[0]["px"].ToString());
                }
                if (ds.Tables[0].Rows[0]["sot"].ToString() != "")
                {
                    model.sot = int.Parse(ds.Tables[0].Rows[0]["sot"].ToString());
                }
                model.beizhu = ds.Tables[0].Rows[0]["beizhu"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [tid],[tname],[duty],[tel],[email],[area],[pic],[addtime],[px],[sot],[beizhu] ");
            strSql.Append(" FROM lt_leadTeam ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DataObject.Query(strSql.ToString());
        }

        #endregion  成员方法
    }
}